﻿using System;

using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.Animations;
using System.Diagnostics;
using Silvermoon.Controls.Forms;
using Silvermoon.UI;
using FileExplorer;
using System.Collections.Generic;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Testing.Pages;
using Silvermoon.Navigation;
using Silvermoon.Debug;

namespace Testing
{
    public class MainPage : Page
    {
        public static MainPage Instance;

        private ListView itemView;
        public static FontSprite labelFont;
        public static FontSprite boldFont;
        protected Toolbar tb;


        static MainPage()
        {
            labelFont = FontSprite.Regular;
            boldFont = FontSprite.Bold;
        }

        public MainPage()
            : base()
        {
            this.KeepAlive = true;
            Title = "SILVERMOON  DEMOS";
            SetTransition(300, TransitionMask.Fade, 1, TransitionMask.Fade);
            CreateToolbar();

            Controls.Add(new GradientControl());
            TextColor = Color.White;
            Color = Color.Empty;
            itemView = new ListView();
            itemView.Margin = new Margin(8, 8, 8, 8);
            //            ListViewGroup group = new ListViewGroup { ItemsPerRow = 1 };
            List<AppItem> list = new List<AppItem>();

            list.Add(new AppItem("Date Picker", typeof(DatePickerPage)));
            list.Add(new AppItem("Pickers", typeof(PickerDemo)));
            list.Add(new AppItem("Controls (Styled)", typeof(DefaultControlsWindow)));
            list.Add(new AppItem("Controls (Metro)", typeof(MetroControlsDemo)));
            list.Add(new AppItem("ListView", typeof(ListViewPage)));
            list.Add(new AppItem("ListView with columns and style", typeof(ColumnListViewPage)));
            list.Add(new AppItem("TextBox Demo #1", typeof(TextboxDemo)));
            list.Add(new AppItem("Textbox Demo #2", typeof(TextBoxDemo2)));
            list.Add(new AppItem("Panorama View", typeof(HubDemo)));
            list.Add(new AppItem("Pivot", typeof(PivotDemo)));
            list.Add(new AppItem("Tabs", typeof(TabDemo)));
            list.Add(new AppItem("Popup menu", typeof(PopupMenuDemo)));
            list.Add(new AppItem("Bubble", typeof(BubbleDemo)));
            list.Add(new AppItem("Calculator", typeof(Calculator)));
            list.Add(new AppItem("Buttons", typeof(ButtonsDemo)));
            list.Add(new AppItem("Label", typeof(LabelTest)));
            list.Add(new AppItem("TextBox Demo #3", typeof(TextBox2Page)));
            list.Add(new AppItem("StackPanel", typeof(StackPanelPage)));
            list.Add(new AppItem("DropDownBox", typeof(StackPanelPage)));
            list.Add(new AppItem("Grid", typeof(GridPage)));

            list.Sort((a, b) => string.Compare(a.Text, b.Text, true));
            itemView.ItemsSource = list;
            Controls.Add(itemView);
            DebuggerWindow.Monitor = true;
        }

        //[Conditional("DEBUG")]
        private void CreateToolbar()
        {
            tb = new Toolbar();
            ToolbarButton btn = new ToolbarButton { Content = "?" };
            btn.Tap += (s, e) =>
                {
                    Silvermoon.Debug.DebuggerWindow.Monitor = true;
                    Silvermoon.Debug.DebuggerWindow.ShowDebugInformation();
                };
            tb.Buttons.Add(btn);
            Toolbar = tb;
        }

        /// <summary>
        /// Prefetch some fonts which are later used, to inhibit delays:
        /// </summary>
        private static void PrefetchFonts()
        {
            FontSprite font = FontSprite.Bold;
            font = FontSprite.Regular;
            font = FontSprite.FromData(GlFontFamily.GenericSansSerif, 24, FontStyle.Bold, false);
            font = FontSprite.FromData(GlFontFamily.GenericSansSerif, 24, FontStyle.Regular, false);
        }

        protected override void OnHome(HandledEventArgs e)
        {
            MessageDialog.Show("Confirmation", "Close Demo Application?",
                () => Screen.Close(),
                null);
        }

        public override object OnSaveViewState()
        {
            return itemView.YOffset;
        }

        public override void OnLoadViewState(object viewState)
        {
            if (viewState is int)
            {
                itemView.YOffset = (int)viewState;
            }
        }
    }


    public class AppItem : TextItem
    {
        private Type windowType;

        public AppItem(string title, Type windowType)
            : base()
        {
            Label.LineMode = LineMode.EndEllipsis;
            Label.Alignment = Alignment.Center;
            this.windowType = windowType;
            this.Text = title;
            //Background = new PlasticButtonShape();
            // Color = PhoneColors.Dark;
            //Padding = Margin.Centered;
            Margin = new Margin(4);
            //this.Text = windowType.Name;
            Height = 80;
            TextColor = PhoneColors.White;
            //this.Background = PhoneColors.Orange;
        }



        protected override void OnTap(TapEventArgs e)
        {
            base.OnTap(e);
            DemoWindowBase demo = Activator.CreateInstance(windowType) as DemoWindowBase;
            Page page = this.Window as Page;
            page.NavigateTo(demo);
        }

    }
}
